﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Framework;


namespace SchemaMonitor.html
{
	public enum ETimeline 
	{
		PostText,
		Shared,     //SharedWhat
		ProfilePic,
		CommentedOn,
		NowFriendsWith, //FriendsWith
		Liked,
		TaggedIn,   //WithWho
		PostPics,
		AlbumPics,
		WasAddedTo,
		StatusUpdate,
		ChangedTheir,
		UpdatedTheir,
		AddedPhotoToMyTimeline,
		WentTo,
		Joined,
		InvitedTo,
		Recommend,
		PostedVideoTimeline,
		Other
	}


	[Serializable]
	public class CTimeline
	{
		public string Date;
		public string Intro;
		public string Comment;

		//construct
		public CTimeline() { }

		//properties
		public ETimeline  Type_
		{
			get
			{
				if (IsPostText) return ETimeline.PostText;
				if (NowFrields) return ETimeline .NowFriendsWith;
				if (WasAddedTo) return ETimeline.WasAddedTo;
				if (IsComment) return ETimeline .CommentedOn;
				if (IsProfilePic) return ETimeline .ProfilePic;
				if (IsShared) return ETimeline .Shared;
				if (IsWith) return ETimeline .TaggedIn;
				if (IsJoined) return ETimeline.Joined;
				if (IsWentTo) return ETimeline.WentTo;
				if (IsStatusUpdate) return ETimeline.StatusUpdate;
				if (IsAddedPhotoToMyTimeline) return ETimeline.AddedPhotoToMyTimeline;
				if (IsChanged) return ETimeline.ChangedTheir;
				if (IsUpdated) return ETimeline.UpdatedTheir;
				if (IsInvitedTo) return ETimeline.InvitedTo;
				if (IsRecommends) return ETimeline.Recommend;
				if (IsPostedVideoTimeline) return ETimeline.PostedVideoTimeline;
				if (PhotosAdded > 0) return ETimeline .PostPics;
				if (PhotosAddedToAlbum > 0) return ETimeline .AlbumPics;
				if (IsLike) return ETimeline.Liked;
				if (IsPostText) return ETimeline.PostText;
				return ETimeline .Other;
			}
		}

		
		public bool IsPostText { get { return null == Intro; } }
		public bool IsInvitedTo { get { return (Intro ?? string.Empty).Contains(" invited to "); } }
		public bool IsRecommends { get { return (Intro ?? string.Empty).Contains(" recommends "); } }
		public bool IsJoined { get { return (Intro ?? string.Empty).Contains(" joined "); } }
		public bool IsWith { get { return (Intro ?? string.Empty).Contains(" with "); } }
		public bool IsLike { get { return (Intro ?? string.Empty).Contains(" likes "); } }
		public bool NowFrields { get { return (Intro ?? string.Empty).Contains("now friends"); } }
		public bool WasAddedTo { get { return (Intro ?? string.Empty).Contains("was added to"); } }		
		public bool IsShared { get { return (Intro ?? string.Empty).Contains("shared"); } }
		public bool IsProfilePic { get { return (Intro ?? string.Empty).Contains("profile picture"); } }
		public bool IsComment { get { return (Intro ?? string.Empty).Contains("commented on an article"); } }
		public bool IsStatusUpdate { get { return (Intro ?? string.Empty).Contains("updated their status"); } }
		public bool IsChanged { get { return (Intro ?? string.Empty).Contains(" changed their "); } }
		public bool IsUpdated { get { return (Intro ?? string.Empty).Contains(" updated their "); } }
		public bool IsWentTo { get { return (Intro ?? string.Empty).Contains(" went to "); } }
		public bool IsPostedVideoTimeline { get { return (Intro ?? string.Empty).Contains(" posted a video to their timeline"); } }		
		public bool IsAddedPhotoToMyTimeline
		{
			get
			{
				var s = (Intro ?? string.Empty);
				return s.Contains(" added a new photo to ")
					&& s.EndsWith(" timeline.");
			}
		}
		

		public int PhotosAdded
		{
			get
			{
				int count = 0;
				if (null == this.Intro)
					return count;

				string s = this.Intro ?? string.Empty;
				if (s.Contains("added"))
					if (s.Contains("new photo"))
					{
						var i = s.IndexOf("added ") + 6;
						var j = s.IndexOf(" ", i);
						s = s.Substring(i, j - i);
						int.TryParse(s, out count);
						if (s == "a")
							count = 1;
					}
				return count;
			}
		}
		public int PhotosAddedToAlbum
		{
			get
			{
				int count = 0;
				if (null == this.Intro)
					return count;

				string s = this.Intro ?? string.Empty;
				if (s.Contains("added"))
					if (s.Contains("new photo"))
						if (s.Contains("to the album"))
						{
							var i = s.IndexOf("added ") + 6;
							var j = s.IndexOf(" ", i);
							s = s.Substring(i, j - i);
							int.TryParse(s, out count);
							if (s == "a")
								count = 1;
						}
				return count;
			}
		}

		public string SharedWhat
		{
			get
			{
				if (!IsShared)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, "shared ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string WithWho
		{
			get
			{
				if (!IsWith)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " with ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string FriendsWith
		{
			get
			{
				if (!NowFrields)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " and ");
				if (ss.Count > 1)
				{
					ss = CUtilities.SplitOn(ss[1], " are ");
					if (ss.Count > 1)
						return ss[0];
				}
				return string.Empty;
			}
		}
		public string AddedTo
		{
			get
			{
				if (!NowFrields)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " was added to ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string LikesWho
		{
			get
			{
				if (!IsLike)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " likes ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string ChangedWhat
		{
			get
			{
				if (!IsChanged)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " changed their ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string UpdatedWhat
		{
			get
			{
				if (!IsChanged)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " updated their ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string WentToWhat
		{
			get
			{
				if (!IsChanged)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " went to ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string JoinedWhat
		{
			get
			{
				if (!IsChanged)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " joined ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string InvitedToWhat
		{
			get
			{
				if (!IsChanged)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " invited to ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}
		public string RecommendsWhat
		{
			get
			{
				if (!IsChanged)
					return string.Empty;
				var ss = CUtilities.SplitOn(this.Intro, " recommends ");
				if (ss.Count > 1)
					return ss[1];
				return string.Empty;
			}
		}



		public DateTime AsDate
		{
			get
			{
				return CHtml.ToDate(this.Date);
			}
		}
	}
}
